﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using GameStateManagement;
using TermitesMagnets.Targets;

namespace TermitesMagnets
{
    public abstract class Level
    {
        protected List<Color> colors;
        protected List<Color> magnetsColors;
        protected Board board;
        private List<DrawableGameComponent> components;
        protected List<Termite> termites;
        protected List<TargetRect> targets;
        private List<TargetRect> humanTargets;

        public List<TargetRect> HumanTargets
        {
            get { return humanTargets; }
        }
        private List<TargetRect> aiTargets;

        public List<TargetRect> AiTargets
        {
            get { return aiTargets; }
        }
        protected List<Magnet> magnets;
        protected List<Pwu> pwus;
        protected GameplayScreen gameplayScreen;
        private GameBackground background;

        public Board Board { get { return board; } }
        public List<Termite> Termites { get { return termites; } set { termites = value; } }
        public List<TargetRect> Targets { get { return targets; } set { targets = value; } }
        public List<Magnet> Magnets { get { return magnets; } set { magnets = value; } }
        public List<Pwu> Pwus { get { return pwus; } set { pwus = value; } }

        public Level(GameplayScreen game)
        {
            this.targets = new List<TargetRect>();
            this.humanTargets = new List<TargetRect>();
            this.aiTargets = new List<TargetRect>();
            this.gameplayScreen = game;
            components = new List<DrawableGameComponent>();
            board = new Board();
        }

        public void AddComponent(DrawableGameComponent gameComponent)
        {
            components.Add(gameComponent);
        }

        public virtual void Restart()
        {
            ClearComponenets();
            this.targets = new List<TargetRect>();
            this.humanTargets = new List<TargetRect>();
            this.aiTargets = new List<TargetRect>();
            components = new List<DrawableGameComponent>();
            this.background = new GameBackground(Game1.GlobalGame);
            board = new Board();
        }

        public void Activate()
        {
            Restart();
            Game1.GlobalGame.Components.Add(background);
            foreach (DrawableGameComponent gameComponent in board.Obstacles)
            {
                Game1.GlobalGame.Components.Add(gameComponent);
            }
            foreach (DrawableGameComponent gameComponent in components)
            {
                Game1.GlobalGame.Components.Add(gameComponent);
                if (gameComponent is ScreenManager)
                    gameComponent.DrawOrder = 0;
                else if (gameComponent is GameBackground)
                    gameComponent.DrawOrder = 1;
                else if (gameComponent is SmokePlumeParticleSystem || gameComponent is ExplosionParticleSystem)
                    gameComponent.DrawOrder = 2;
                else if (gameComponent is Termite || gameComponent is Magnet || gameComponent is Obstacle)
                    gameComponent.DrawOrder = 3;
                else if (gameComponent is Pwu)
                    gameComponent.DrawOrder = 4;
                else if (gameComponent is ExplosionSmokeParticleSystem)
                    gameComponent.DrawOrder = 5;
            }

        }

        public void ClearComponenets()
        {
            foreach (GameComponent gameComponent in components)
            {
                Game1.GlobalGame.Components.Remove(gameComponent);
            }

            foreach (GameComponent gameComponent in board.Obstacles)
            {
                Game1.GlobalGame.Components.Remove(gameComponent);
            }
            Game1.GlobalGame.Components.Remove(background);
            components.Clear();
        }

        public void ClearLevel()
        {
            ClearComponenets();
        }


    }
}
